<?php

/**
 * Abbrevia: App::setting('App')
 */
function app_setting($setting = "")
{
	if ( ! $setting )
	{
		return App::setting("App");
	}
	
	return App::setting("App.{$setting}");
}








/**
 * Functioni per le categorie.
 */
function get_categories()
{
	return Cache::store('list', 'category', Database::get_instance()->where('status', '1')->get('categories'));
}

function get_category($category_id)
{
	return Cache::store('category', $category_id, Database::get_instance()->where('id', $category_id)->get('categories'));
}

function category_link($id, $title, $attributes = array())
{
	$link = "category/{$id}/" . url::title($title) . app_setting('url_prefix');

	return html::anchor($link, $title, $attributes);
}

function category_url($id, $title)
{
	$url = "category/{$id}/" . url::title($title) . app_setting('url_prefix');

	return url::site($url);
}






/**
 * Functioni per gli articoli.
 *
 * @param integer Passa il limite.
 * @return array
 */
function get_articles($limit = NULL)
{
	$articles = Cache::store('list', 'article', Database::get_instance()->where('status', '1')->get('articles', $limit, 0));
	
	foreach ( $articles AS $article )
	{
		$article['category'] = get_category($article['category_id']);
		
		$rows[] = $article;
	}
	
	unset($articles);

	return $rows;
}

function get_article($article_id)
{
	$article = Database::get_instance()->where(array('status' => '1', 'id' => $article_id))->get('articles');
	
	$article['category'] = get_category($articla['category_id']);
	
	return $article;
}

function article_link($id, $title, $category_title, $attributes = array())
{
	$link = "article/{$id}/" . url::title($category_title) . "/" . url::title($title) . app_setting('url_prefix');

	return html::anchor($link, $title, $attributes);
}

function article_url($id, $category_title, $title)
{
	$url = "article/{$id}/" . url::title($category_title) . "/" . url::title($title) . app_setting('url_prefix');

	return url::site($url);
}








/**
 * Functioni per i commenti.
 */
function get_comments($article_id)
{
	return Cache::store('list', "comments_{$article_id}", Database::get_instance()->where(array('status' => '1', 'article_id' => $article_id))->get('comments'));
}

function get_comment($comment_id)
{
	return Cache::store('comment', $comment_id, Database::get_instance()->where(array('status' => '1', 'id' => $comment_id))->get('comments'));
}















/**
 * Functioni per gli autori.
 */
function get_authors()
{
	return Cache::store('list', 'author', Database::get_instance()->where('status', '1')->get('authors'));
}

function get_author($author_id)
{
	return Cache::store('author', $author_id, Database::get_instance()->where(array('status' => '1', 'id' => $author_id))->get('authors'));
}